﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace TheoryOn.Mvc.Models
{
    public class Construct
    {
        public int ID { get; set; }
        public float X { get; set; }
        public float Y { get; set; }
        public string Title { get; set; }

        public int? AntecedentID { get; set; }
        public Construct Antecedent { get; set; }

        public int? ConsequenceID { get; set; }
        public Construct Consequence { get; set; }
    }

    public class Article
    {
        public Article()
        {
            Constructs = new List<Construct>();
        }

        public string Color { get; set; }

        public Dictionary<string, object> NodeOptions { get; set; }

        public Dictionary<string, object> EdgeOptions { get; set; }

        public List<Construct> Constructs { get; set; }

        public IList ToVisNodes()
        {
            return Constructs.Select(a => new
            {
                id = a.ID,
                label = a.Title,
                color = Color
            }).ToList();
        }

        public IList ToVisEdges()
        {
            var dictionary = new Dictionary<int, int>();
            foreach (var con in Constructs)
            {
                if (con.AntecedentID != null)
                    dictionary[con.AntecedentID.Value] = con.ID;
                if (con.ConsequenceID != null)
                    dictionary[con.ID] = con.ConsequenceID.Value;
            }
            return dictionary.OrderBy(a => a.Key).Select(a => new
            {
                from = a.Key,
                to = a.Value,
                arrows = "to"
            }).ToList();
        }

        public static IList ToVisNodes(IEnumerable<Article> articles)
        {
            if (articles.Any())
            {
                var result = default(IList);
                foreach (var article in articles)
                {
                    var current = article.ToVisNodes();
                    if (result == null)
                    {
                        result = current;
                    }
                    else
                    {
                        foreach (var item in current)
                            result.Add(item);
                    }
                }
                return result;
            }
            return new List<object>();
        }

        public static IList ToVisEdges(IEnumerable<Article> articles)
        {
            if (articles.Any())
            {
                var result = default(IList);
                foreach (var article in articles)
                {
                    var current = article.ToVisEdges();
                    if (result == null)
                    {
                        result = current;
                    }
                    else
                    {
                        foreach (var item in current)
                            result.Add(item);
                    }
                }
                return result;
            }
            return new List<object>();
        }
    }
}